package com.dalianpai.gulimall.ware.dao;

import com.dalianpai.gulimall.ware.entity.WareSkuEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品库存
 * 
 * @author dalianpai
 * @email dalianpai@163.com
 * @date 2020-05-25 22:03:23
 */
@Mapper
public interface WareSkuDao extends BaseMapper<WareSkuEntity> {

    @Update("UPDATE `wms_ware_sku` SET stock=stock+#{skuNum} WHERE sku_id=#{skuId} AND ware_id=#{wareId}")
    void addStock(@Param("skuId") Long skuId, @Param("wareId") Long wareId, @Param("skuNum") Integer skuNum);

    @Select("SELECT sum(stock-stock_locked) from wms_ware_sku where sku_id = #{id}")
    Long getSkuStock(@Param("id")Long id);

    @Select("select ware_id from wms_ware_sku where sku_id= #{skuId} AND stock-stock_locked > 0")
    List<Long> listWareIdHasSkuStock(@Param("skuId")Long skuId);

    @Update("update wms_ware_sku set stock_locked = stock_locked+#{num} where sku_id=#{skuId} AND ware_id = #{wareId} AND stock-stock_locked >= #{num}")
    Long lockSkuStock(@Param("skuId") Long skuId, @Param("wareId") Long wareId, @Param("num") Integer num);

    @Update("update wms_ware_sku set stock_locked = stock_locked - #{skuNum} where sku_id=#{skuId} AND ware_id = #{wareId}")
    void unLockStock(@Param("skuId") Long skuId, @Param("wareId") Long wareId, @Param("skuNum") Integer skuNum);
}
